//
//  YHJWHomepageCtry.m
//  test
//
//  Created by 李彦宏 on 2018/3/23.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "YHJWHomepageCtry.h"
#import "JWHomePageHead.h"
#import "JWHomePageCell.h"
#import "JWUserManager.h"
#import "JWStudentInfoCtry.h"
#import "SDUserModel.h"
#import "PYSearch.h"
#import "JWHongSearchResultVC.h"


@interface YHJWHomepageCtry ()<UITableViewDelegate,UITableViewDataSource,PYSearchViewControllerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *contenArry;
@property (nonatomic, strong) SDUserModel *model;
@property (nonatomic, strong) NSMutableArray *UrgentArry;


@end

@implementation YHJWHomepageCtry
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setHongHead];
    //!tableView懒加载要用self.tableView
    [self.view addSubview:self.tableView];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self setdata];
    }];

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setdata];
    
}
#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _contenArry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JWHomePageCell *cell=[JWHomePageCell cellwithTableview:tableView];
    cell.Smode=self.contenArry[indexPath.row];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    JWStudentInfoCtry *vc=[[JWStudentInfoCtry alloc]init];
    self.model=self.contenArry[indexPath.row];
    vc.title=[NSString stringWithFormat:@"%@的信息",self.model.name];
    [JWUserManager saveSD:self.model];
    [self.navigationController pushViewController:vc animated:YES];
    
 
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}
- (void)setHongHead{
    JWHomePageHead *head=[[JWHomePageHead alloc]initWithFrame:CGRectMake(0, Navigation_Bar_Height, ScreenWidth,70)];
    [head.StSearchBtn addTarget:self action:@selector(SearchBAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:head];

}
//学员搜索功能
- (void)SearchBAction{
    NSArray *Hotarr = @[@"田涛",@"朱圳",@"李彦宏",@"肖欢芯",@"游鹤亭",@"严戈闲",@"陈炽",@"赵延"];
    NSMutableArray *HoMutabarry=[[NSMutableArray alloc]init];
    
    if(self.UrgentArry.count<8){
        HoMutabarry=[NSMutableArray arrayWithArray:Hotarr];
    }else{
        for(int i=0;i<8;i++){
            self.model=self.UrgentArry[i];
            [HoMutabarry addObject:self.model.name];
        }
    }
    PYSearchViewController *searchController = [PYSearchViewController searchViewControllerWithHotSearches:HoMutabarry searchBarPlaceholder:@"学生姓名 专业" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
        JWHongSearchResultVC *vc = [[JWHongSearchResultVC alloc]init];
        vc.title=[NSString stringWithFormat:@"%@的搜索结果",searchText];
        
        vc.searchText = searchText;
        [searchViewController.navigationController pushViewController:vc animated:YES];
    }];
    // 2. 设置风格
    searchController.hotSearchStyle = PYHotSearchStyleBorderTag;
    searchController.hotSearchTitle = @"推荐搜索";
    
    searchController.searchSuggestionHidden=YES;
    // 3. 设置代理
    searchController.delegate = self;
    
    // 4. 跳转到搜索控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchController];
    [self presentViewController:nav  animated:NO completion:nil];
    
}
#pragma -mark ———————————————————加载首页数据————————————————————
- (void)setdata{
    NSDictionary *dic=@{
                        @"jwid":JWid,
                        @"jwpassword":JWpassword,
                        };
    [[AFNetworkTool shareManager]postJSONWithUrl:JW_HomePage parmas:dic successData:^(id json) {
        [self.tableView.mj_header endRefreshing];
        [self.UrgentArry removeAllObjects];
        [self.contenArry removeAllObjects];
        NSArray *arry=json[@"studentlist"];
        for(int i=0;i<arry.count;i++){
            self.model=[SDUserModel yy_modelWithJSON:arry[i]];
            [JWUserManager saveSD:self.model];
            [self.contenArry addObject:self.model];
            if(![arry[i][@"statu"] isEqualToString:@"0"]){
                [self.UrgentArry addObject:self.model];
            }
        }
        __weak typeof(self) weakslf = self;
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakslf.tableView reloadData];
        });
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络连接错误"];
         [self.tableView.mj_header endRefreshing];
    }];
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,70+Navigation_Bar_Height, ScreenWidth, ScreenHeight-70-Navigation_Bar_Height)style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.estimatedRowHeight=0;
        _tableView.estimatedSectionHeaderHeight=0;
        _tableView.estimatedSectionFooterHeight=0;
        _tableView.delegate = self;
        _tableView.rowHeight=45;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (NSMutableArray *)contenArry{
    if(!_contenArry){
        _contenArry=[[NSMutableArray alloc]init];
    }
    return _contenArry;
}
- (NSMutableArray *)UrgentArry{
    if(!_UrgentArry){
        _UrgentArry=[[NSMutableArray alloc]init];
    }
    return _UrgentArry;
    
}
@end
